package com.wlscale.roadsystem.wiget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.easy.component.component.toast.EasyToast;
import com.wlscale.roadsystem.R;
import com.wlscale.roadsystem.base.LZBaseActivity;
import com.wlscale.roadsystem.modular.TestActivtiy;
import com.wlscale.roadsystem.wiget.SwipeMenuListView.SwipeMenu;
import com.wlscale.roadsystem.wiget.SwipeMenuListView.SwipeMenuCreator;
import com.wlscale.roadsystem.wiget.SwipeMenuListView.SwipeMenuItem;
import com.wlscale.roadsystem.wiget.SwipeMenuListView.SwipeMenuListView;


/**
 * 描述: {系统校准}
 * 作者: jack-song
 * 创建日期: 2017/11/7 10:04
 * 修改日期: 2017/11/7
 * 版本: {版本}
 */
public class CalibrationListAlertDialog extends Dialog {
    /**
     *
     */
    private Context mContext;
    /**
     * 提示
     */
    private TextView tvTitle;
    /**
     * 左侧按钮
     */
    private Button btnLeft;
    /**
     * 添加修正节点
     */
    private Button btn_add_correct_node;

    /**
     * 左侧文字
     */
    private CharSequence leftText;
    /**
     *
     */
    private SwipeMenuListView listView;
    /**
     *
     */
    private SysAdapter adapter;
    /**
     *
     */
    private CharSequence title;

    /**
     * 左侧按钮
     */
    private View.OnClickListener leftOnClickListener;

    public CalibrationListAlertDialog(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    public CalibrationListAlertDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
    }

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_weight_correct_node_manage);
        tvTitle = findViewById(R.id.tv_title);
        btnLeft = findViewById(R.id.btn_left);
        btn_add_correct_node = findViewById(R.id.btn_add_correct_node);
        if (TextUtils.isEmpty(title)) {
            tvTitle.setVisibility(View.GONE);
            tvTitle.setText("");
        } else {
            tvTitle.setVisibility(View.VISIBLE);
            tvTitle.setText(title);
        }
        if (null == leftOnClickListener) {
            btnLeft.setVisibility(View.GONE);
        } else {
            btnLeft.setVisibility(View.VISIBLE);
            if (!TextUtils.isEmpty(leftText)) {
                btnLeft.setText(leftText);
            }
            btnLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    leftOnClickListener.onClick(view);
                    dismiss();
                }
            });
        }

        btn_add_correct_node.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((LZBaseActivity)mContext).showAddCorrectNoteAlert("测试","确定",new View.OnClickListener(){

                    @Override
                    public void onClick(View view) {

                    }
                },"取消",new View.OnClickListener(){

                    @Override
                    public void onClick(View view) {

                    }
                });
            }
        });

        listView = (SwipeMenuListView) findViewById(R.id.listView);
        adapter = new SysAdapter();
        listView.setAdapter(adapter);
        SwipeMenuCreator creator = new SwipeMenuCreator() {

            @Override
            public void create(SwipeMenu menu) {
                // Create different menus depending on the view type
                createMenu1(menu);
            }
        };
        listView.setMenuCreator(creator);
        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                return false;
            }
        });

    }

    private void createMenu1(SwipeMenu menu) {
        SwipeMenuItem item1 = new SwipeMenuItem(
                mContext);
        item1.setBackground(R.color.button_red_disabled_edge);
        item1.setWidth(dp2px(100));
        item1.setIcon(R.mipmap.ic_delete);
        menu.addMenuItem(item1);
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager wm = window.getWindowManager();
        Point windowSize = new Point();
        wm.getDefaultDisplay().getSize(windowSize);
        window.getAttributes().width = (int) (windowSize.x * 0.8f);
        window.getAttributes().height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);

    }

    public void setTitle(CharSequence title) {
        this.title = title;
    }

    public void setLeftText(CharSequence leftText) {
        this.leftText = leftText;
    }


    public void setLeftOnClickListener(View.OnClickListener leftOnClickListener) {
        this.leftOnClickListener = leftOnClickListener;
    }


    class SysAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 10;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            // menu type count
            return 1;
        }

        @Override
        public int getItemViewType(int position) {
            // current menu type
            return 1;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(mContext,
                        R.layout.item_correct_var_list, null);
                new ViewHolder(convertView);
            }
            ViewHolder holder = (ViewHolder) convertView.getTag();
            return convertView;
        }

        class ViewHolder {

            public ViewHolder(View view) {
                view.setTag(this);
            }
        }
    }

    public static class Builder {
        /**
         * 上下文
         */
        private Context context;
        /**
         * 提示
         */
        private CharSequence title;
        /**
         * 左侧文字
         */
        private CharSequence leftText;
        /**
         * 左侧按钮
         */
        private View.OnClickListener leftOnClickListener;

        public Builder() {
        }

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setTitle(CharSequence title) {
            this.title = title;
            return this;
        }


        public Builder setLeftText(CharSequence leftText) {
            this.leftText = leftText;
            return this;
        }


        public Builder setLeftOnClickListener(View.OnClickListener leftOnClickListener) {
            this.leftOnClickListener = leftOnClickListener;
            return this;
        }


        public CalibrationListAlertDialog builder() {
            CalibrationListAlertDialog loadingDialog = new CalibrationListAlertDialog(context, R.style.loading_dialog);
            loadingDialog.setTitle(title);
            loadingDialog.setLeftText(leftText);
            loadingDialog.setLeftOnClickListener(leftOnClickListener);
            return loadingDialog;
        }
    }

    private int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mContext.
                getResources().getDisplayMetrics());
    }
}
